
//公告
const app = getApp(); //获取全局app.js
const api = require('../../utils/api.js')
Page({
  data: {
    imgbox: '',//上传图片
    logs: [],
    title:'默认标题',//相册标题
    date:'',
    actionSheetHidden: true,
    actionSheetItems: ['item1', 'item2', 'item3', 'item4']
  }, 
  onShow() {
    let date = new Date(),
        month = date.getMonth() + 1,
        day = date.getDate();
        month = month < 10 ? '0' + month : month ;
        day = day < 10 ? '0' + day : day ;
        this.setData({
          date:month + '月' + day + '日'
        })
  },
  actionSheetTap: function (e) {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    });
  },
  actionSheetChange: function (e) {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    });
  },
  bindItemTap: function (e) {
    console.log('tap ' + e.currentTarget.dataset.name);
  },
  // 删除照片 &&
  imgDelete1: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.deindex;
    let imgbox = this.data.imgbox;
    imgbox.splice(index, 1)
    that.setData({
      imgbox: imgbox
    });
  }, // 上传图片 &&&
  uploadImage:function(e){
    var that = this;
    console.log(that)
    var num = app.config.image_max 
    
    // var num = app.config.image_max;
    // console.log(num)
    wx.chooseImage({
      count: num,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res) 
        console.log(res.tempFilePaths) 
        var imgbox = res.tempFilePaths  
        that.setData({
          imgbox: imgbox
        });
        
        for (var i = 0; i < imgbox.length; i++) {
          let base64 = wx.arrayBufferToBase64(imgbox[i]);
          console.log(imgbox[i])
          console.log(base64)
          wx.uploadFile({
            //url: 'https://shop.enjoyinglearning.com/centenzilu/Plugin/uploadImage', //仅为示例，非真实的接口地址
            url: app.config.api_url + 'law.html',
            // url:'http://192.168.1.44:6019/centenzilu/Plugin/uploadIma', //仅为示例，非真实的接口地址
            filePath: imgbox[i],
            name: 'fishfile',
            formData: {
              'action': 'test'
            },
            success: function (res) {
              console.log(res.data)
              var obj = JSON.parse(res.data);
             
              // wx.downloadFile({
              //   url: obj[0],
              //   success(res) {
                  // wx.getFileSystemManager().readFile({
                  //   filePath: obj[0], //选择图片返回的相对路径
                  //   encoding: 'base64', //编码格式
                  //   success: res => { //成功的回调
                  //     console.log('data:image/png;base64,' + res.data)
                  //   }
                  // })
                // }
              // })

              // wx.getFileSystemManager().readFile({
              //   filePath: imgbox[i],
              //   encodeing: 'base64',
              //   success: function (ress) {
              //     console.log(ress)
              //   }
              // })
              console.log(obj)
              var data = res.data
              //do something
            }
          })
        }
        
      }
    }); 
    
  },

  upd:function(e){
    var that = this;

     console.log(that.data.imgbox) 
    var tempFilePaths = that.data.imgbox;
     
    

    
  },
   


  addPic1: function (e) {
    var imgbox = this.data.imgbox;
    console.log(imgbox)
    var picid = e.currentTarget.dataset.pic;
    console.log(picid)
    var that = this;
    var n = 9;
    if (9 > imgbox.length > 0) {
      n = 9 - imgbox.length;
    } else if (imgbox.length == 9) {
      n = 1;
    }
    wx.chooseImage({
      count: n, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // console.log(res.tempFilePaths)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths

        if (imgbox.length == 0) {
          imgbox = tempFilePaths
        } else if (9 > imgbox.length) {
          imgbox = imgbox.concat(tempFilePaths);

        } else {
          imgbox[picid] = tempFilePaths[0];
        }
        that.setData({
          imgbox: imgbox
        });
      }
    })
  }, 
  upconfirm: function () {
    this.up();
  },
  up: function () {
    var that = this;
    data = {
      openid: app.openid,
      program_id: app.program_id,
      only_num: only_num
    }
    wx.uploadFile({
      url: api.api('192.168.1.44:6019/centenzilu/Plugin/uploadImage'),//'pg.php/Aishen/upload_photo',
      filePath: that.data.img_arr[i],
      name: 'image', //文件对应的参数名字(key)
      formData: data,  //其它的表单信息
      success: function (res) {
        console.log(res)
        console.log(i);
      }, complete: function (complete) {
        console.log(complete)
        i++
        if (i == that.data.img_arr.length) {
          util.request('192.168.1.44:6019/centenzilu/Plugin/uploadImage', 'post', { 'only_num': only_num }, '正在加载数据', function (res) {
            console.log(res)
            if (res.data.state == 1) {
              wx.showModal({
                title: '提示',
                content: '提交成功!',
                success: function (res) {
                  that.onLoad()
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '提交失败,请重新提交!',
              })
            }
          })
        } else if (i < that.data.img_arr.length) {//若图片还没有传完，则继续调用函数
          that.up()
        }
      }
    })
  }, 
  // 上传
  upimg: function () {
    var that = this;
    console.log(that.data.img_arr)
    if (this.data.img_arr.length < 12) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths)
          })
          num = that.data.img_arr.length
        }
      })
    } else {
      wx.showToast({
        title: '最多上传12张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  }, 
  upload: function () {
    var that = this
    //  var status = wx.getStorageSync("status");
    //  adds["status"]=status;
    console.log(adds);
    console.log(tempPaths);
    for (var i = 0; i < tempPaths.length; i++) {
      wx.uploadFile({
        url: 'http://localhost:80/api/text_api.php',
        filePath: tempPaths[i],
        name: "content" + i,
        formData: adds,
        success: function (res) {
          if (res) {
            wx.showToast({
              title: '上传成功',
              duration: 3000
            });
          }
        }
      })
    }
  },
  


})